package com.jiayi.cms.service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.doudou.core.container.ArrUtil;
import com.doudou.core.container.ListUtil;
import com.jiayi.api.JiayiNewsTypeService;
import com.jiayi.common.utils.PageUtils;
import com.jiayi.common.utils.poi.ExcelUtil;
import com.jiayi.data.entity.response.JiayiNewsTypeDTO;
import com.jiayi.data.entity.response.JiayiProductDTO;
import com.jiayi.data.entity.response.JiayiProductTypeDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.jiayi.common.core.page.TableDataInfo;
import com.jiayi.cms.mapper.JiayiNewsMapper;
import com.jiayi.data.entity.schema.JiayiNews;
import com.jiayi.data.entity.excel.JiayiNewsExcel;
import com.jiayi.data.entity.bo.JiayiNewsBO;
import com.jiayi.data.entity.request.JiayiNewsRequest;
import com.jiayi.data.entity.request.JiayiNewsSearchRequest;
import com.jiayi.data.entity.request.JiayiNewsSearchPageRequest;
import com.jiayi.data.entity.response.JiayiNewsDTO;
import com.jiayi.data.converter.JiayiNewsConverter;
import com.jiayi.api.JiayiNewsService;
import com.jiayi.common.developutil.ConvertUtils;
import com.github.pagehelper.PageInfo;
import com.jiayi.common.utils.DateUtils;
import javax.servlet.http.HttpServletResponse;
import com.jiayi.common.constant.HttpStatus;
import static com.jiayi.common.utils.SecurityUtils.getUserIdStr;

/**
 * 新闻动态Service业务层处理
 *
 * @author zzx
 * @date 2024-11-26
 */
@Service
@Slf4j
public class JiayiNewsServiceImpl implements JiayiNewsService {
    @Autowired private JiayiNewsMapper jiayiNewsMapper;

    @Autowired private JiayiNewsConverter jiayiNewsconverter;

    @Autowired private JiayiNewsTypeService jiayiNewsTypeService;

    /**
     * 查询新闻动态
     *
     * @param id 新闻动态主键
     * @return 新闻动态
     */
    @Override
    public JiayiNewsDTO selectJiayiNewsById(Long id) {
        JiayiNews jiayiNews = jiayiNewsMapper.selectJiayiNewsById(id);
        JiayiNewsDTO jiayiNewsDTO = jiayiNewsconverter.convertToJiayiNewsDTO(jiayiNews);
        jiayiNewsDTO.setTypeDTO(jiayiNewsTypeService.selectJiayiNewsTypeById(jiayiNews.getTypeId()));
        return jiayiNewsDTO;
    }

    /**
     * 查询新闻动态列表
     *
     * @param request 新闻动态
     * @return 新闻动态
     */
    @Override
    public TableDataInfo selectJiayiNewsList(JiayiNewsSearchPageRequest request) {
        PageUtils.startPage(request.getPageNum(),request.getPageSize());
        JiayiNews jiayiNews = jiayiNewsconverter.convertToJiayiNews(request);
        List<JiayiNews> list = jiayiNewsMapper.selectJiayiNewsList(jiayiNews);
        List<JiayiNewsDTO> dtos = ConvertUtils.convert(list, jiayiNewsconverter::convertToJiayiNewsDTO);
        if (ListUtil.isNotEmpty(dtos)){
            List<Long> typeIds = dtos.stream().map(JiayiNewsDTO::getTypeId).distinct().collect(Collectors.toList());
            List<JiayiNewsTypeDTO> jiayiNewsTypeDTOS = jiayiNewsTypeService.selectJiayiNewsTypeListByIds(ArrUtil.toArray(typeIds, Long.class));
            Map<Long, JiayiNewsTypeDTO> typeDTOMap = jiayiNewsTypeDTOS.stream().collect(Collectors.toMap(JiayiNewsTypeDTO::getId, o -> o));
            for (JiayiNewsDTO dto : dtos) {
                dto.setTypeDTO(typeDTOMap.get(dto.getTypeId()));
            }
        }
        return TableDataInfo.builder().code(HttpStatus.SUCCESS).msg("查询成功").rows(dtos).total(new PageInfo(list).getTotal()).build();
    }

    @Override
    public List<JiayiNewsDTO> selectJiayiNewsHomeList() {
        JiayiNewsSearchRequest jiayiNewsSearchRequest = new JiayiNewsSearchRequest();
        jiayiNewsSearchRequest.setIsHome(1);
        List<JiayiNews> jiayiNews = selectJiayiNewsList(jiayiNewsSearchRequest);
        List<JiayiNewsDTO> dtos = ConvertUtils.convert(jiayiNews, jiayiNewsconverter::convertToJiayiNewsDTO);
        if (ListUtil.isNotEmpty(dtos)){
            List<Long> typeIds = dtos.stream().map(JiayiNewsDTO::getTypeId).distinct().collect(Collectors.toList());
            List<JiayiNewsTypeDTO> jiayiNewsTypeDTOS = jiayiNewsTypeService.selectJiayiNewsTypeListByIds(ArrUtil.toArray(typeIds, Long.class));
            Map<Long, JiayiNewsTypeDTO> typeDTOMap = jiayiNewsTypeDTOS.stream().collect(Collectors.toMap(JiayiNewsTypeDTO::getId, o -> o));
            for (JiayiNewsDTO dto : dtos) {
                dto.setTypeDTO(typeDTOMap.get(dto.getTypeId()));
            }
        }
        return dtos;
    }

    /**
     * 查询新闻动态列表 不分页
     *
     * @param request 新闻动态
     * @return 新闻动态集合
     */
    public List<JiayiNews> selectJiayiNewsList(JiayiNewsSearchRequest request){
        JiayiNews jiayiNews = jiayiNewsconverter.convertToJiayiNews(request);
        List<JiayiNews> list = jiayiNewsMapper.selectJiayiNewsList(jiayiNews);
        return list;
    }

    /**
     * excel 导出 全量
     *
     * @param request 新闻动态
     */
    public void export(JiayiNewsSearchRequest request, HttpServletResponse response){
        JiayiNews jiayiNews = jiayiNewsconverter.convertToJiayiNews(request);
        List<JiayiNews> list = jiayiNewsMapper.selectJiayiNewsList(jiayiNews);
        List<JiayiNewsExcel> excelList = ConvertUtils.convert(list, jiayiNewsconverter::convertToJiayiNewsExcel);
        ExcelUtil<JiayiNewsExcel> util = new ExcelUtil<JiayiNewsExcel>(JiayiNewsExcel.class);
        util.exportExcel(response, excelList, "新闻动态数据");
    }

    /**
     * 新增新闻动态
     *
     * @param request 新闻动态
     * @return 结果
     */
    @Override
    public int insertJiayiNews(JiayiNewsRequest request) {
        JiayiNews jiayiNews = jiayiNewsconverter.convertToJiayiNews(request);
        jiayiNews.setCreateBy(getUserIdStr());
        jiayiNews.setCreateTime(DateUtils.getNowDate());
        return jiayiNewsMapper.insertJiayiNews(jiayiNews);
    }

    /**
     * 修改新闻动态
     *
     * @param request 新闻动态
     * @return 结果
     */
    @Override
    public int updateJiayiNews(JiayiNewsRequest request) {
        JiayiNews jiayiNews = jiayiNewsconverter.convertToJiayiNews(request);
        return jiayiNewsMapper.updateJiayiNews(jiayiNews);
    }

    /**
     * 批量删除新闻动态
     *
     * @param ids 需要删除的新闻动态主键
     * @return 结果
     */
    @Override
    public int deleteJiayiNewsByIds(Long[] ids) {
        return jiayiNewsMapper.deleteJiayiNewsByIds(ids);
    }

    /**
     * 删除新闻动态信息
     *
     * @param id 新闻动态主键
     * @return 结果
     */
    @Override
    public int deleteJiayiNewsById(Long id) {
        return jiayiNewsMapper.deleteJiayiNewsById(id);
    }
}
